<template>
    <div>
        <!--          面包屑-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!--        卡片区域-->
        <el-card class="box-card">
            <el-row :gutter="20">
<!--                <el-col :span="4"> <div class="chaxun">根据文章内容查询:</div></el-col>-->
<!--                <el-col :span="6">-->
<!--                    <el-input-->
<!--                            placeholder="请输入内容"-->
<!--                            prefix-icon="el-icon-search"-->
<!--                            v-model="neiirong">-->
<!--                    </el-input>-->
<!--                </el-col>-->
<!--                <el-col :span="1" >-->
<!--                    <el-button type="success" icon="el-icon-search" @click="chaneirong" plain></el-button>-->
<!--                </el-col>-->
<!--                <el-col :span="3" :push="1"> <div class="chaxun">根据日期查询:</div></el-col>-->
<!--                <el-col :span="10" :push="1">-->
<!--&lt;!&ndash;                    文章日期范围&ndash;&gt;-->
<!--                    <el-date-picker-->
<!--                            v-model="value1"-->
<!--                            type="daterange"-->
<!--                            range-separator="至"-->
<!--                            start-placeholder="开始日期"-->
<!--                            end-placeholder="结束日期">-->
<!--                    </el-date-picker>-->
<!--                </el-col>-->
<!--            </el-row>-->
<!--            <el-row :gutter="20">-->
<!--                <el-col :span="4"> <div class="chaxun">根据文章标题:</div></el-col>-->
<!--                <el-col :span="6">-->
<!--                    <el-input-->
<!--                            placeholder="请输入内容"-->
<!--                            prefix-icon="el-icon-search"-->
<!--                            v-model="biaoti">-->
<!--                    </el-input>-->
<!--                </el-col>-->
<!--                <el-col :span="1" >-->
<!--                    <el-button type="success" icon="el-icon-search" @click="chabiaoti" plain></el-button>-->
<!--                </el-col>-->
<!--                <el-col :span="4" :push="1"> <div class="chaxun">根据文章作者:</div></el-col>-->
<!--                <el-col :span="6" :push="1">-->
<!--                    <el-input-->
<!--                            placeholder="请输入内容"-->
<!--                            prefix-icon="el-icon-search"-->
<!--                            v-model="zuozhe">-->
<!--                    </el-input>-->
<!--                </el-col>-->
<!--                <el-col :span="1" :push="1">-->
<!--                    <el-button type="success" icon="el-icon-search" @click="chazuozhe" plain></el-button>-->
<!--                </el-col>-->
<!--            </el-row>-->
<!--            <el-row :gutter="20">-->
<!--                <el-col :span="3"> <div class="chaxun">排序方式:</div></el-col>-->
<!--                <el-col :span="4">-->
<!--                    <el-select v-model="value" placeholder="请选择">-->
<!--                        <el-option-->
<!--                                v-for="item in paixv"-->
<!--                                :key="item.value"-->
<!--                                :label="item.label"-->
<!--                                :value="item.value">-->
<!--                        </el-option>-->
<!--                    </el-select>-->
<!--                </el-col>-->
<!--                <el-col :span="4" :push="5">-->
<!--                    <el-button type="danger" @click="clear" plain>清空</el-button>-->
<!--                </el-col>-->
            </el-row>
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="文章作者" prop="userName">
                    <el-input
                            v-model="queryParams.article.userName"
                            placeholder="模糊搜索"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="文章标题" prop="title">
                    <el-input
                            v-model="queryParams.article.title"
                            placeholder="模糊搜索"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="文章内容" prop="title">
                    <el-input
                            v-model="queryParams.article.content"
                            placeholder="模糊搜索"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="发布时间" prop="issueTime">
                    <el-date-picker
                            v-model="issueTime"
                            end-placeholder="结束日期"
                            range-separator="至"
                            start-placeholder="开始日期"
                            type="datetimerange"
                            @change="handleIssueTime">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="排序方式" >
                    <el-select placeholder="请选择排序方式" v-model="option" @change="handleOption" >
                        <el-option label="按照点赞量👇" value="1"></el-option>
                        <el-option label="按照点赞量👆" value="2"></el-option>
                        <el-option label="按照收藏量👇" value="3"></el-option>
                        <el-option label="按照收藏量👆" value="4"></el-option>
                        <el-option label="按照浏览量👇" value="5"></el-option>
                        <el-option label="按照浏览量👆" value="6"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row style="object-position: right">
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <!--            文章数据展示区域-->
            <el-table v-loading="loading" :data="adminArticleList">
                <el-table-column
                        type="index"
                        label="#"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="标题"
                        align="center" >
                </el-table-column>
                <el-table-column label="文章内容" align="center" width="300px">
                    <template slot-scope="{ row }">
                        <el-button :plain="true" @click="openHTML(row.content)" style="height: 30px" type="text">
                            <div
                                    id="content"
                                    v-html="row.content"
                                    style="
                                         overflow: hidden;
                                         text-overflow: ellipsis;
                                         white-space: nowrap;
                                         width: 100%;
                                         height: 30px;
                                         text-align: center;"
                            ></div>
                        </el-button>
                    </template>

                </el-table-column>
                <el-table-column
                        prop="userName"
                        label="作者"
                        align="center" >
                </el-table-column>
                <el-table-column
                        prop="time"
                        label="发布时间"
                        align="center" >
                </el-table-column>
                <el-table-column
                        prop="countGetLike"
                        label="点赞量"
                        align="center" >
                </el-table-column>
                <el-table-column
                        prop="countCollect"
                        label="收藏量"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="countLook"
                        label="浏览量"
                        align="center"
                >
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                        >修改</el-button>
                        <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="shangchu(scope.row.id)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--     分页     -->
            <el-pagination
                    v-show="total>0"
                    :page-size="queryParams.pageSize"
                    :pager-count="11"
                    :current-page.sync="queryParams.pageNum"
                    :total="total"
                    @current-change="getList"
            >
            </el-pagination>
            <!-- 添加或修改article对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <el-form-item label="文章作者" prop="userName">
                        <el-tag type="info" effect="plain">{{form.userName}}</el-tag>
                    </el-form-item>
                    <el-form-item label="文章标题" prop="title">
                        <el-input v-model="form.title" placeholder="请输入文章标题" />
                    </el-form-item>
                    <el-form-item label="文章内容">
                        <editor v-model="form.content" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="发布时间" prop="time">
                        <div class="block">
                            <el-date-picker
                                    v-model="form.time"
                                    type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    placeholder="选择日期时间">
                            </el-date-picker>
                        </div>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>

</template>

<script>
    // import marked from "marked";
    // import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";

    import Editor from '@/components/Editor';


    export default {
        name: "wenzhang",
        components: { Editor },
        data(){
            return{
                adminArticleList:[],
                filadminArticleList:[],
                neiirong:'',
                biaoti:'',
                zuozhe:'',
                paixv:[{
                    value: '1',
                    label: '按点赞量升序'
                }, {
                    value: '2',
                    label: '按点赞量降序'
                }, {
                    value: '3',
                    label: '按收藏量升序'
                }, {
                        value: '4',
                        label: '按收藏量降序'
                    }],
                value:'',
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                },
                // 弹出对话框标题
                title: "",
                // 修改对话框开关控制
                open :false,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    article:{
                        userName: null,
                        title: null,
                        content: null,
                        time: null,
                        timeMin: null,
                        timeMax: null,
                    },
                    sidx: 'id',
                    sord: 'DESC',

                },
                // 排序选择
                option: null,
                issueTime: null,
            }
        },
        created() {
            this.getList()
        },


        methods:{
            // 查询帖子列表
            async getList() {
                this.loading = true;
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                const {data: response} = await this.$http.get("/article/adminArticleList?pageNum="+this.queryParams.pageNum+'&'+"pageSize="+this.queryParams.pageSize+'&'+
                    "article.userName="+this.queryParams.article.userName+'&'+"article.content="+this.queryParams.article.content+'&'+
                    "article.title="+this.queryParams.article.title+'&'+ "article.timeMin="+
                    this.queryParams.article.timeMin+'&'+"article.timeMax="+this.queryParams.article.timeMax+'&'+ "sidx="+
                    this.queryParams.sidx+'&'+"sord="+this.queryParams.sord);
                this.adminArticleList = response.data.article;
                this.total = response.data.total;
                this.loading = false;
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.get('article/'+ id).then(response => {
                    this.form = response.data.data.article;
                    console.log(this.form);
                    this.title = "修改文章内容";
                    this.open = true;
                });
            },
            resetQuery() {
                this.queryParams={
                    pageNum: 1,
                        pageSize: 10,
                        article:{
                        userName: null,
                            title: null,
                            content: null,
                            timeMin: null,
                            timeMax: null,
                    },
                    sidx: 'id',
                    sord: 'DESC',

                },
                // 排序选择
                this.option=null,
                this.issueTime=null,
                this.handleQuery();
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            // this.form.time = this.form.date1 + " "+this.form.date2;
                            console.log(this.form);
                            console.log(this.form.time);
                            const token = sessionStorage.getItem("token");
                            this.$http.defaults.headers.common['Authorization'] = token;
                            console.log(this.form)
                            this.$http.put('article', this.form).then(response => {
                                if (response.data.code === 200) {
                                    this.$message({showClose: true, message: "修改成功", type: "success"})
                                    this.open = false;
                                    this.getList()
                                }
                            });
                        }
                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    userId: null,
                    userName: null,
                    title: null,
                    content: null,
                    time: null
                };
                // this.resetForm("form");
            },
            // 打开文章详情
            openHTML(content) {
                this.$message({
                    dangerouslyUseHTMLString: true,
                    message: content,
                    duration: 1000,
                });
            },
            async getadminArticleList() {
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                const {data: res} = await this.$http.get('article/adminArticleList')
                console.log(res.data)
                this.adminArticleList = res.data.article
                this.filadminArticleList = this.adminArticleList
            },
            clear(){
                    this.value='',
                    this.neiirong='',
                    this.biaoti='',
                    this.zuozhe='',
                    this.filadminArticleList=this.adminArticleList
            },
            async shangchu(id){
                console.log( typeof id)
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                await this.$http.delete(`article/${id}`)
                this.getList()
            },
            // tableRowClassName({rowIndex}) {
            //     if (rowIndex === 1) {
            //         return 'warning-row';
            //     } else if (rowIndex === 3) {
            //         return 'success-row';
            //     }
            //     return '';
            // }
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 排序操作赋值 */
            handleOption(option){
                if(option == 1){
                    this.queryParams.sidx = "count_get_like",
                    this.queryParams.sord = "DESC"
                }else if(option == 2){
                    this.queryParams.sidx = "count_get_like",
                        this.queryParams.sord = "ASC"

                }else if(option == 3){
                    this.queryParams.sidx = "count_collect",
                        this.queryParams.sord = "DESC"

                }else if(option == 4){
                    this.queryParams.sidx = "count_collect",
                        this.queryParams.sord = "ASC"
                }else if(option == 5){
                    this.queryParams.sidx = "count_look",
                        this.queryParams.sord = "DESC"

                }else if(option == 6){
                    this.queryParams.sidx = "count_look",
                        this.queryParams.sord = "ASC"
                }
            },
            /** 切割时间 */
            handleIssueTime(time) {
                this.queryParams.article.timeMin = this.parseTime(time[0], '{y}-{m}-{d}')
                this.queryParams.article.timeMax = this.parseTime(time[1], '{y}-{m}-{d}')
            },
            /** 时间处理 */
            parseTime(time, pattern) {
                if (arguments.length === 0 || !time) {
                    return null
                }
                const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
                let date
                if (typeof time === 'object') {
                    date = time
                } else {
                    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
                        time = parseInt(time)
                    } else if (typeof time === 'string') {
                        time = time.replace(new RegExp(/-/gm), '/');
                    }
                    if ((typeof time === 'number') && (time.toString().length === 10)) {
                        time = time * 1000
                    }
                    date = new Date(time)
                }
                const formatObj = {
                    y: date.getFullYear(),
                    m: date.getMonth() + 1,
                    d: date.getDate(),
                    h: date.getHours(),
                    i: date.getMinutes(),
                    s: date.getSeconds(),
                    a: date.getDay()
                }
                const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
                    let value = formatObj[key]
                    // Note: getDay() returns 0 on Sunday
                    if (key === 'a') {
                        return ['日', '一', '二', '三', '四', '五', '六'][value]
                    }
                    if (result.length > 0 && value < 10) {
                        value = '0' + value
                    }
                    return value || 0
                })
                return time_str
            }
        },

    }
</script>

<style scoped>
    .el-breadcrumb{
        margin-bottom: 12px;

    }
    .el-card{
        box-shadow: 0 1px 1px rgba(0,0,0,0.15);
    }
    .chaxun{

        height: 40px;
        text-indent: 1em;
        line-height: 2.5em;
        vertical-align: middle;
    }

</style>
